<template>
  <div>
    <!--搜索栏-->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.roleName" placeholder="角色名称" clearable />
          <el-button type="primary" round icon="el-icon-search" @click="getRoleList">查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button type="primary" icon="el-icon-plus" circle @click="openAddUI(null)" />
        </el-col>
      </el-row>
    </el-card>
    <!--结果，分页-->
    <el-card>
      <template>
        <el-table :data="roleList" stripe style="width: 100%">
          <el-table-column prop="id" label="角色id" width="180" />
          <el-table-column prop="roleName" label="角色名称" width="180" />
          <el-table-column prop="roleDesc" label="角色描述" />
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openAddUI(scope.row.id)" />
              <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteRole(scope.row)" />
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <!--分页组件-->
    <el-card>
      <el-pagination
        :current-page="searchModel.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!--用户信息编辑对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
      <el-form ref="roleForm" :model="roleForm">
        <el-form-item label="角色名称" prop="roleName" :label-width="formLabelWidth">
          <el-input v-model="roleForm.roleName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input v-model="roleForm.roleDesc " autocomplete="off" />
        </el-form-item>
        <el-form-item label="权限设置" prop="menuIdList" :label-width="formLabelWidth">
          <el-tree
            ref="menuRef"
            :data="menuList"
            :props="menuProps"
            show-checkbox
            node-key="id"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import roleApi from '@/api/role'
import menuApi from '@/api/menuManage'

export default {
  data() {
    return {
      menuIdList: [],
      menuList: [],
      menuProps: {
        children: 'children',
        label: 'title'
      },
      formLabelWidth: '130px',
      roleForm: {},
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        page: 1,
        pageSize: 10
      },
      roleList: []
    }
  },
  created() {
    this.getAllMenuList()
    this.getRoleList()
  },
  methods: {
    getAllMenuList() {
      menuApi.getAllMenu().then(res => {
        this.menuList = res.data
      })
    },
    deleteRole(role) {
      this.$confirm(`您是否确认该删除角色${role.roleName}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        roleApi.deleteRole(role.id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.getRoleList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveRole() {
      // 触发表单验证
      this.$refs['roleForm'].validate((valid) => {
        if (valid) {
          const checkedKeys = this.$refs.menuRef.getCheckedKeys()
          const halfCheckedKeys = this.$refs.menuRef.getHalfCheckedKeys()
          this.roleForm.menuIdList = checkedKeys.concat(halfCheckedKeys)
          // 提交请求给后台
          roleApi.saveRole(this.roleForm).then(res => {
            // 成功提示，关闭对话框，刷新数据
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.dialogFormVisible = false
            this.getRoleList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.roleForm = {}
      this.$refs.roleForm.clearValidate()
      this.$refs.menuRef.setCheckedKeys([])
    },
    openAddUI(id) {
      this.dialogFormVisible = true
      if (id == null) {
        this.title = '新增角色'
      } else {
        this.title = '修改角色'
        roleApi.getRoleById(id).then(res => {
          this.roleForm = res.data
          // eslint-disable-next-line no-eval
          const a = eval(res.data.menuIdList)

          this.$refs.menuRef.setCheckedKeys(a)
        })
      }
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getRoleList()
    },
    handleCurrentChange(page) {
      this.searchModel.page = page
      this.getRoleList()
    },
    getRoleList() {
      roleApi.getRoleList(this.searchModel).then(res => {
        this.roleList = res.data.rows
        this.total = res.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}
</style>
